<template>
  <div>
    <div class="login-pad">
      <h2>
        <img src="/img/icons/Logo2.png" alt="">
        经销商平台
      </h2>
      <p>
        <input type="text" placeholder="用户账号" v-model="userNo">
      </p>
      <p>
        <input type="password" placeholder="密码" v-model="password" @keyup.enter="login">
      </p>
      <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
      <button @click="login">
        →
      </button>

    </div>
    <div class="login-bottom">
      &copy;徐宇凡
    </div>
  </div>
</template>

<script lang="ts">
import {reactive, toRefs} from 'vue'
import {ILoginInfo} from '@/interfaces/Login'
import {login} from '@/HttpRequests/LoginRequest'
import {useRouter} from 'vue-router'

export default {
  setup() {
    const router = useRouter()
    const loginInfo: ILoginInfo = reactive({
      userNo: '',
      password: '',
      errorMessage: '',
      login: async () => {
        try {
          if (!loginInfo.userNo) {
            loginInfo.errorMessage = '账号不能为空'
            return
          }

          if (!loginInfo.password) {
            loginInfo.errorMessage = '密码不能为空'
            return
          }

          const res = await login({
            customerNo: loginInfo.userNo,
            password: loginInfo.password
          })
          if (res != null) {
            localStorage['cno'] = loginInfo.userNo
            localStorage['token'] = res
            await router.push('/layoutMain')
          }
        } catch (error: any) {
          loginInfo.errorMessage = '账号或密码错误'
        }
      }
    })
    return {...toRefs(loginInfo)}
  }
}
</script>

<style lang="scss" scoped>
.login-pad {
  text-align: center;
  width: 60%;
  margin: auto;
  margin-top: 26%;

  h2 {
    font-weight: normal;
    margin-bottom: 30px;

    img {
      display: inline-block;
      width: 36px;
      height: 36px;
      background: transparent;
      border-radius: 18px;
      vertical-align: -9px;
    }
  }

  p {
    width: 100%;
    margin-top: 20px;

    input {
      width: 100%;
      box-sizing: border-box;
      height: 36px;
      border-radius: 18px;
      border: 0 none;
      background-color: #f0f0f0;
      text-align: center;
    }
  }

  button {
    margin-top: 36px;
    width: 60px;
    height: 60px;
    border-radius: 30px;
    border: 0 none;
    background-color: rgb(79, 137, 245);
    color: #fff;
    font-size: 26px;
    font-weight: bold;
  }
}

.error-message {
  color: red;
  margin-bottom: 15px;
  text-align: center;
}

.login-bottom {
  position: absolute;
  bottom: 10px;
  text-align: center;
  width: 100%;
  font-size: 14px;
}
</style>
